import json
import os
from User_Management.user_classes import Administrator
from Course_Management_and_Enrollment.course_task_classes import Course

class CourseConsistencyChecker:
    @staticmethod
    def check_and_fix_course_status(admin, all_courses):
        """检查并修复课程状态与all_courses列表的一致性"""
        fixed_courses = []
        
        # 检查所有已批准的课程是否都在管理员的all_courses列表中
        for course in all_courses:
            if course.status == "approved" and course not in admin.all_courses:
                admin.all_courses.append(course)
                fixed_courses.append(f"已将状态为'approved'的课程 '{course.course_name}' 添加到管理员all_courses列表")
            
            # 检查所有在管理员all_courses列表中的课程状态是否为approved
            if course in admin.all_courses and course.status != "approved":
                course.status = "approved"
                fixed_courses.append(f"已将课程 '{course.course_name}' 的状态更新为'approved'")
                
        # 检查管理员all_courses列表中的课程是否都存在于系统中
        for course in admin.all_courses[:]:
            if course not in all_courses:
                admin.all_courses.remove(course)
                fixed_courses.append(f"已从管理员all_courses列表移除不存在的课程")
        
        return fixed_courses

    @staticmethod
    def run_consistency_check(admin, lms):
        """运行一致性检查并返回修复结果"""
        results = []
        results.extend(CourseConsistencyChecker.check_and_fix_course_status(admin, lms.courses))
        return results
